import { LogAPI } from '../index'
import { get, post, del } from '@/utils/request'

export interface Log {
  id: string
  type: 'info' | 'warning' | 'error'
  module: string
  action: string
  operator: string
  ip: string
  details: string
  time: string
}

export interface LogParams {
  page?: number
  pageSize?: number
  type?: string
  module?: string
  operator?: string
  startTime?: string
  endTime?: string
  keyword?: string
}

export interface CreateLogParams {
  type: 'info' | 'warning' | 'error'
  module: string
  action: string
  details: string
}

export const LogService = {
  async getLogs(params: LogParams): Promise<{
    total: number
    items: Log[]
  }> {
    return get(LogAPI.list, params)
  },

  async createLog(params: CreateLogParams): Promise<Log> {
    return post<Log>(LogAPI.create, params)
  },

  async deleteLog(id: string): Promise<void> {
    return del(LogAPI.delete(id))
  },

  async clearLogs(before: string): Promise<void> {
    return post(LogAPI.clear, { before })
  },

  async exportLogs(params: LogParams): Promise<Blob> {
    return get(LogAPI.export, {
      ...params,
      responseType: 'blob',
    })
  },
}
